$(function () {
    init();
})

function init() {
    FastClick.attach(document.body);
    var now = new Date();
    $("#today").html(addDate(now, 0));
    $("#yesterday").html(addDate(now, -1));
    myTab("#tabBt", "#tabPage", getQueryString("index"), function (index) {
        search(index);
    });
    bindEvent();
    loadCookie();
}

function loadCookie() {
    $("#history").html("<div class=\"button noInfo\"><span>无查询记录</span></div>");
    equipmemtService.queryEquipmentHis(function (history) {
        if (history != null) $("#history").html("");
        for (var code in history) {
            var name = history[code].name.split("-");
            var fullname = history[code].name.substring(name[0].length + name[1].length + 2);
            var button = $("<div class=\"button\" style='background-image: url(" + history[code].externalPhotos + ")' onclick='window.location.href = \"/edetail/" + code + "\"' value='" + code + "'><span class=\"type\">" + name[0] + "</span><br><span>" + fullname + "</span></div>");
            $("#history").append(button);
        }
        $(".button").height(($(".button").width() - 10) + "px");

    })
}

function loadUInformation0(serverData, group) {
    $(serverData).each(function (index, item) {
        var name = item.name.split('-');
        var row = $(' <div class="row">' +
            '                        <div class="title">' + name[1] + name[2] + '</div>' +
            '                        <div class="subTitle fa fa-map-marker"> ' + item.usePosition + '</div>' +
            '                        <div class="state"><span class="fa fa-tools"><br>' + baseService.getServerConstNameByCode("state", item.state) + '</span></div>' +
            '                        <div class="secret"><span class="fa fa-lock"><br>' + baseService.getServerConstNameByCode("secrecy", item.secrecy) + '</span></div>' +
            '                        <div class="flex">' +
            '                            <div class="label">领用时间</div>' +
            '                            <div class="value">' + item.wcUTime + '</div>' +
            '                        </div>' +
            '                        <div class="flex">' +
            '                            <div class="label">' + item.useTypeName + '</div>' +
            '                            <div class="value">' + item.useOrgName + item.useStaffName + '</div>' +
            '                        </div>' +
            '                        <div class="flex">' +
            '                            <div class="label">二维码</div>' +
            '                            <div class="value">' + item.QRCodeName + '</div>' +
            '                        </div>' +
            '                    </div>');
        group.append(row);
        row.on("click", function () {
            window.location.href = "/edetail/" + item.QRCodeName;
        })
    })
}

function loadUInformation1(serverData, group) {
    $(serverData).each(function (index, item) {
        var row = $(' <div class="row">' +
            '                        <div class="title">' + item.name + "【" + item.roleName + '】</div>' +
            '                        <div class="subTitle fa fa-map-marker"> ' + getOrgName(item.organizationId, orgInfo.list[0], "") + '</div>' +
            '                        <div class="flex">' +
            '                            <div class="label">联系方式</div>' +
            '                            <div class="value">' + item.telephone + '</div>' +
            '                        </div>' +
            '                        <div class="flex">' +
            '                            <div class="label">出生日期</div>' +
            '                            <div class="value">' + item.birthday + '</div>' +
            '                        </div>' +
            '                        <div class="flex">' +
            '                            <div class="label">其他联系方式</div>' +
            '                            <div class="value">' + item.telephone2 + " " + item.telephone3 + '</div>' +
            '                        </div>' +
            '                    </div>');
        group.append(row);
        row.on("click", function () {
            $("#searchInput").val(item.name + " " + item.telephone);
            $(".tabButtons").children().eq(0).trigger("click");
        })
    })
}

function loadUInformation2(serverData, group) {
    $(serverData).each(function (index, item) {
        var row = $(' <div class="row">' +
            '                        <div class="title"><span class="fa ' + (item.count <= item.warnCount ? "fa-meh" : "fa-smile") + '" style="color:' +
            (item.count <= item.warnCount ? "#cc3f24" : "#14b5cc")
            + '"> ' + (item.brandName || "") + item.eqName + (item.eName || "") + '</span></div>' +
            '                        <div class="subTitle fa fa-map-marker"> ' + item.name + " " + item.position + '</div>' +
            '                        <div class="flex">' +
            '                            <div class="label">当前数量</div>' +
            '                            <div class="value">' + item.count + '</div>' +
            '                        </div>' +
            '                        <div class="flex">' +
            '                            <div class="label">警告数量</div>' +
            '                            <div class="value">' + item.warnCount + '</div>' +
            '                        </div>' +
            '                    </div>');
        group.append(row);
    })
}

function loadUInformation3(serverData, group) {
    $(serverData).each(function (index, item) {
        var row = $(' <div class="row">' +
            '                        <div class="title">' +
            '<span>' + (item.brandName || "") + item.eqName + (item.eName || "") + '</span>' +
            '<span class="fa ' +
            (item.type.startsWith("3330") ? "fa-arrow-up" : "fa-arrow-down") + '" style="float: right; ' +
            '    margin-top:10px;color:' +
            (item.type.startsWith("3330") ? "#14b5cc" : "#cc3f24")
            + '"> ' + item.count + ' </span>' +
            '<span  class="fa beforCount">' + item.beforeCount + ' </span>' +
            '</div>' +
            '                        <div class="subTitle fa fa-map-marker"> ' + item.typeName + " " + item.reason + '</div>' +
            '                        <div class="flex">' +
            '                            <div class="label">生效时间</div>' +
            '                            <div class="value">' + item.updateTime + '</div>' +
            '                        </div>' +
            (item.type.startsWith("3330") ?   '                        <div class="flex">' +
                '                            <div class="label">入库单号</div>' +
                '                            <div class="value">' + item.flowCode + '</div>' +
                '                        </div>' :
                '                        <div class="flex">' +
                '                            <div class="label">领取位置</div>' +
                '                            <div class="value">' + item.usePosition + '</div>' +
                '                        </div>' +

                '                        <div class="flex">' +
                '                            <div class="label">' + item.useTypeName + '</div>' +
                '                            <div class="value">' + item.useOrgName + item.useStaffName + '</div>' +
                '                        </div>' +
                '                        <div class="flex">' +
                '                            <div class="label">申请单号</div>' +
                '                            <div class="value">' + item.gFlowCode + '</div>' +
                '                        </div>' +
                '                        <div class="flex">' +
                '                            <div class="label">出库单号</div>' +
                '                            <div class="value">' + item.flowCode + '</div>' +
                '                        </div>' ) +
            '                    </div>');
        group.append(row);
        row.on("click", function () {
            $("#searchInput").val((item.brandName || "") + " " + item.eqName + " " + (item.eName || ""));
            $(".tabButtons").children().eq(2).trigger("click");
        })
    })
}

var keyWord = "";
var pagesKeyWord = {
    page0: "",
    page1: "",
    page2: "",
    page3: ""
}

function search() {
    keyWord = $("#searchInput").val();
    if (keyWord.trim() == "") {
        $.toptip("请输入搜索关键词，词组之间用空格隔开");
        return;
    }
    $("#searchBox").css("bottom", "-100%");
    var pageIndex = $("#tabBt .active").index();
    if (keyWord != pagesKeyWord["page" + pageIndex]) {
        switch (pageIndex) {
            case 0:
                $("#page0").html("");
                equipmemtService.searchEquipment({keyWord: keyWord}, function (data) {
                    if (data.success) {
                        pagesKeyWord["page" + pageIndex] = keyWord;//sucess以后赋值
                        var serverData = data.datas;
                        loadUInformation0(serverData, $("#page0"));
                    } else {
                        $.alert(data.errorMsg);
                    }
                })
                break;
            case 1:
                $("#page1").html("");
                equipmemtService.searchOrgStaff({keyWord: keyWord}, function (data) {
                    if (data.success) {
                        pagesKeyWord["page" + pageIndex] = keyWord;//sucess以后赋值
                        var serverData = data.datas;
                        loadUInformation1(serverData, $("#page1"));
                    } else {
                        $.alert(data.errorMsg);
                    }
                })
                break;
            case 2:
                $("#page2").html("");
                equipmemtService.searchWarehouse({keyWord: keyWord}, function (data) {
                    if (data.success) {
                        pagesKeyWord["page" + pageIndex] = keyWord;//sucess以后赋值
                        var serverData = data.datas;
                        loadUInformation2(serverData, $("#page2"));
                    } else {
                        $.alert(data.errorMsg);
                    }
                })
                break;
            case 3:
                $("#page3").html("");
                equipmemtService.searchWarehouseChange({keyWord: keyWord}, function (data) {
                    if (data.success) {
                        pagesKeyWord["page" + pageIndex] = keyWord;//sucess以后赋值
                        var serverData = data.datas;
                        loadUInformation3(serverData, $("#page3"));
                    } else {
                        $.alert(data.errorMsg);
                    }
                })
                break;
        }

    }
}

function bindEvent() {
    $('body').on('submit', '#searchBar', function (event) {
        //todo ...

        search();
        // selectUserEquipmentList();
    })

    $("#delete").on("click", function () {
        $.confirm("是否要删除查询记录", "确认删除", function () {
            delCookie("eHistory");
            loadCookie();
        })
    })
    $("#searchInput").on("focus", function () {
        if ($("#searchInput").val() == "") {
            $("#searchBox").css("bottom", "0px");
        }
    })
    $("#searchInput").on("keyup", function () {
        if ($("#searchInput").val() == "") {
            $("#searchBox").css("bottom", "0px");
        }
    })
    $('.exampleBox').on('click', '.example', function (event) {
        var keyword = $(event.target).html();
        var pageIndex = $(event.target).parents(".examples").attr("searchPage");
        $("#searchInput").val(keyword);
        $("#searchText").trigger("click");
        $(".tabButtons").children().eq(pageIndex).trigger("click");
    })
}
